package com.xb.springtest.dao;


import cn.hutool.core.collection.CollectionUtil;
import com.xb.springtest.dto.vo.User;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class UserDao {

    private static final ConcurrentHashMap<Integer, User> userDB = new ConcurrentHashMap<>();

    public void addCategory(User user) {
        userDB.put(user.getId(), user);
    }

    public int updateCategory(User user) {
        if(CollectionUtil.isEmpty(userDB)) {
            return -1;
        }
        User entity = userDB.get(user.getId());
        if(null == entity) {
            return -2;
        }
        userDB.put(user.getId(), entity);
        return 1;
    }

    public List<User> UserList(String name) {
        List<User> list = new ArrayList<>();
        for(User entity : userDB.values()){
            if(name!=null) {
                if(entity.getName().equals(name)) {
                    list.add(entity);
                }
            } else {
                list.add(entity);
            }
        }
        return list;
    }

    public void updateCategory(int id) {
        userDB.remove(id);
    }

    public User getCategory(int id) {
        return userDB.get(id);
    }
}
